package com.dtsheep.redis.controller.user;

import com.dtsheep.redis.po.user.User;
import com.dtsheep.redis.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * UserController
 *
 * <p>
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/19
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private RedisUtil redisUtil;


    /**
     * 字符串保存
     * 重复set会覆盖原始值
     */
    @GetMapping("/string/save")
    public void stringSaveOrUpdate(@RequestParam("num") Integer num){
        redisUtil.set("userId:1", "访问次数: "+ num);
        log.info("字符串保存");
    }

    /**
     * 列表保存
     */
    @GetMapping("/list/save")
    public void listSave(@RequestParam("num") Integer num){
        User user1 = new User();
        user1.setAge(num+66);
        user1.setName("张三");
        User user2 = new User();
        user2.setAge(num+100);
        user2.setName("李四");
        List<User> userList = Arrays.asList(user1, user2);
        redisUtil.setCacheList("loginUser", userList);
        log.info("列表保存");
    }

    /**
     * set 集合保存
     */
    @GetMapping("/set/save")
    public void setSave(@RequestParam("num") Integer num){
        User user1 = new User();
        user1.setAge(num+66);
        user1.setName("张三");
        User user2 = new User();
        user2.setAge(num+100);
        user2.setName("李四");
        Set<User> userSet = new HashSet<User>();
        userSet.add(user1);
        userSet.add(user2);
        redisUtil.setCacheSet("loginUser", userSet);
        log.info("set集合保存");
    }

    /**
     * zset 有序集合保存
     */
    @GetMapping("/zset/save")
    public void zsetSave(@RequestParam("num") Integer num){
        User user1 = new User();
        user1.setAge(num+66);
        user1.setName("张三");
        User user2 = new User();
        user2.setAge(num+100);
        user2.setName("李四");
        Set<User> userSet = new HashSet<>();
        redisUtil.setCacheSet("loginUser", userSet);
        log.info("zset集合保存");
    }
}
